package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.FundAccountFlowDao;
import com.xbongbong.saas.domain.entity.FundAccountFlowEntity;
import com.xbongbong.saas.model.FundAccountFlowModel;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.action.support.WriteRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 资金账户流水Model实现
 *
 * @author 徐俊杰
 * @date 2020/3/17 14:09
 * @since v1.0
 */

@Service("fundAccountFlowModel")
public class FundAccountFlowModelImpl implements FundAccountFlowModel {

    @Resource
    private FundAccountFlowDao fundAccountFlowDao;
    @Resource
    private PaasEsModel paasEsModel;

    public static final Logger LOG = LoggerFactory.getLogger(FundAccountFlowModelImpl.class);


    @Override
    public Integer insert(FundAccountFlowEntity entity) throws XbbException {
        //处理时间
        long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        //MySQL
        Integer insert = fundAccountFlowDao.insert(entity);
        //ES
        InsertDTO insertDTO = getSaveDTO(entity, IndexTypeEnum.IDX_FUND_ACCOUNT_FLOW);
        paasEsModel.insert(insertDTO);
        return insert;
    }

    @Override
    public Integer update(FundAccountFlowEntity entity) throws XbbException {
        //MySQL
        Integer update = fundAccountFlowDao.update(entity);
        //ES
        InsertDTO insertDTO = getSaveDTO(entity,IndexTypeEnum.IDX_FUND_ACCOUNT_FLOW);
        paasEsModel.update(insertDTO);
        return update;
    }

    @Override
    public List<FundAccountFlowEntity> findEntitys(Map<String, Object> param) {
        return fundAccountFlowDao.findEntitys(param);
    }

    @Override
    public Integer updateBatch(List<FundAccountFlowEntity> list, String corpid) throws XbbException {
        Integer integer = fundAccountFlowDao.updateBatch(list, corpid);
        //数据库数据同步到es
        updateBatchEs(list);
        return integer;
    }

    @Override
    public void updateBatchEs(List<FundAccountFlowEntity> list) throws XbbException {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<InsertDTO> inserts = new ArrayList<>();
        for (FundAccountFlowEntity e : list) {
            InsertDTO insertDTO = getSaveDTO(e, IndexTypeEnum.IDX_FUND_ACCOUNT_FLOW);
            inserts.add(insertDTO);
        }
        //默认不抛出
        paasEsModel.updateBatch(inserts, WriteRequest.RefreshPolicy.WAIT_UNTIL, false);
    }

    @Override
    public void insertBatch(List<FundAccountFlowEntity> list) throws XbbException {
        fundAccountFlowDao.insertBatch(list);
        //ES数据插入
        List<InsertDTO> insertList = new ArrayList<>();
        for (FundAccountFlowEntity item : list) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
            insertDTO.setEsId(item.getCorpid() + "_" + item.getId());
            item.setId(item.getId());
            insertDTO.setSource(BeanUtil.convertBean2Map(item));
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_FUND_ACCOUNT_FLOW);
            insertList.add(insertDTO);
        }
        paasEsModel.insertBatch(insertList);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return fundAccountFlowDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public List<FundAccountFlowEntity> getFlowListByType(String corpid, Integer type, Collection<Long> sheetIdIn) {
        if (Objects.isNull(type) || CollectionUtils.isEmpty(sheetIdIn)) {
            return new ArrayList<>();
        }
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put(StringConstant.CORPID, corpid);
        params.put("type", type);
        params.put("sheetIdIn", sheetIdIn);
        return findEntitys(params);
    }

    /**
     * 找到资金账户中重复的单据，为刷脚本使用
     *
     * @param fundParams
     * @return
     */
    @Override
    public List<FundAccountFlowEntity> findRepeatSheetList(Map<String, Object> fundParams) {
        return fundAccountFlowDao.findRepeatSheetList(fundParams);
    }

    @Override
    public FundAccountFlowEntity getLatestFlowEntity(String corpid, Long date) {
        return fundAccountFlowDao.getLatestFlowEntity(corpid,date);
    }

    /**
     * 格式化ES保存数据
     *
     * @param entity 资金账户流水实体
     * @param indexTypeEnum ES索引
     * @return com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO
     * @throws XbbException 异常
     * @author 徐俊杰
     * @date 2020/3/17 16:42
     * @since v1.0
     */
    private InsertDTO getSaveDTO(FundAccountFlowEntity entity, IndexTypeEnum indexTypeEnum) throws XbbException {
        InsertDTO insertDTO = new InsertDTO();
        insertDTO.setEsId(entity.getCorpid() + "_" + entity.getId());
        insertDTO.setSource(BeanUtil.convertBean2Map(entity, false, true));
        insertDTO.setIndexTypeEnum(indexTypeEnum);
        return insertDTO;
    }
}
